#include "../decoder_conf.h"


#if DECODER_THREAD == DECODER_THREAD_POSIX
#include "../decoder_thread.h"

#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>

using ThreadFunction = void *(*)(void *);

void *DecoderThread::semCreate(int value)
{
    sem_t *sem = new sem_t;;

    sem_init(sem, 0, value);

    return sem;
}

void DecoderThread::semPost(void *sem)
{
    sem_post((sem_t *)sem);
}

void DecoderThread::semWait(void *sem)
{
    sem_wait((sem_t *)sem);
}

void DecoderThread::semDestroy(void *sem)
{
    sem_destroy((sem_t *)sem);
    delete (sem_t *)sem;
}

void *DecoderThread::threadCreate(void *function, void *arg, size_t stackSize)
{
    pthread_t threadId;

    (void)stackSize;
    pthread_create(&threadId, nullptr, (ThreadFunction)function, arg);

    return (void *)threadId;
}

void DecoderThread::threadJoin(void *thread)
{
    pthread_join((pthread_t)thread, nullptr);
}

void DecoderThread::threadExit(void *thread)
{
    (void)thread;
    pthread_exit(nullptr);
}

void DecoderThread::mSleep(uint32_t ms)
{
    usleep(ms * 1000);
}

#endif
